package com.gzw.dms.utils;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.List;

public class SysApplication {
    private static long exitTime = 0;
    private List<Activity> mList = new LinkedList<Activity>();
    private static SysApplication instance;

    private SysApplication() {
    }

    public synchronized static SysApplication getInstance() {
        if (null == instance) {
            instance = new SysApplication();
        }
        return instance;
    }

    // add Activity  
    public void addActivity(Activity activity) {
        mList.add(activity);
    }

    public void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    public void exitIndex(int index) {
        try {
            Activity activity = mList.get(mList.size()-index);
            activity.finish();
        } catch (Exception e) {

        }
    }

    public void onLowMemory() {
//        super.onLowMemory();     
        System.gc();
    }

    public Boolean exitApp(Context _mContext) {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(_mContext, "再按一次退出程序。", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {

        }
        return true;
    }
}
